package com.example.Wisdompark.service;

import com.example.Wisdompark.dao.CurrentCargoMapper;
import com.example.Wisdompark.pojo.Current_Cargo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class CurrentCargoImpl implements CurrentCargoService {

    @Autowired
    private CurrentCargoMapper currentCargoMapper;

    @Override
    public List<Current_Cargo> queryAllCargo() {
        return currentCargoMapper.queryAllCargo();
    }

    @Override
    public int insertACargo(Current_Cargo current_cargo) {
        return currentCargoMapper.insertACargo(current_cargo);
    }

    @Override
    public int delACargo(String cargo_num) {
        return currentCargoMapper.delACargo(cargo_num);
    }

    @Override
    public int delCargos(String order_num) {
        return currentCargoMapper.delCargos(order_num);
    }

    @Override
    public int updateCargoState(String order_num, int state) {
        return currentCargoMapper.updateCargoState(order_num, state);
    }


    @Override
    public int delSomeCargo(String cargo_name, int cargo_amount) {
        Current_Cargo current_cargo = queryByCargoName(cargo_name);

        if(current_cargo != null){

            if(current_cargo.getCargo_amount() == cargo_amount)
            currentCargoMapper.updateCargoState(cargo_name,2);
            if(current_cargo.getCargo_amount() > cargo_amount)
                return currentCargoMapper.delSomeCargo(cargo_name, cargo_amount);


        }


        return 0;
    }

    @Override
    public Current_Cargo queryByCargoName(String cargo_name) {
        return currentCargoMapper.queryByCargoName(cargo_name);
    }


    @Override
    public int addCargoAmount(String cargo_name, int cargo_amount) {
        // 根据货物名字判断货物状态，首先根据货物名字获取货物对象
        Current_Cargo current_cargo = currentCargoMapper.queryByCargoName(cargo_name);

        // 获取到货物对象
        if(current_cargo != null){
            // 判断货物状态
            switch (current_cargo.getState()){
                // 状态为0 未审核，增加货物失败
                case 0:{
                    return 0;
                }
                // 状态为1 在库中，正常增加
                case 1:{
                    return currentCargoMapper.addCargoAmount(cargo_name,cargo_amount);
                }
                // 状态为2 已全部出库，先把状态更改为在库中，然后再进行数量的增加
                case 2:{
                    currentCargoMapper.updateCargoState(current_cargo.getOrder_num(),1);
                    return currentCargoMapper.addCargoAmount(cargo_name,cargo_amount);
                }
                default:{

                }
            }
        }

        return 0;
    }

    @Override
    public List<Current_Cargo> queryByOrderNum(String order_num) {
        return currentCargoMapper.queryByOrderNum(order_num);
    }

    @Override
    public List<Current_Cargo> queryCargoByState(Integer state) {
        return currentCargoMapper.queryCargoByState(state);
    }
}
